package com.example.demospringboot.config;


import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import com.google.gson.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import springfox.documentation.spring.web.json.Json;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Autowired
    private TypeResolver resolver;

    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("雇员文档")
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.example.demospringboot.controller.employcontroller"))
                .paths(PathSelectors.regex("/.*"))
                .build().apiInfo(apiEndPointsInfo())
                .tags(new Tag("雇员服务", "所有有关雇员的接口"))
                ;
    }

    @Bean
    public Docket api2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("部门文档")
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.example.demospringboot.controller.depcontroller"))
                .paths(PathSelectors.regex("/.*"))
                .build().apiInfo(apiEndPointsInfo())
                .tags(new Tag("部门服务", "所有有关部门的接口"));
    }

//    @Bean
//    public Docket api3() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .select()
//                .apis(RequestHandlerSelectors
//                        .basePackage("com.example.demospringboot.controller"))
//                .paths(PathSelectors.regex("/.*"))
//                .build().apiInfo(apiEndPointsInfo())
//                .tags(new Tag("所有服务", "所有的接口"));
//    }


    private ApiInfo apiEndPointsInfo() {

        return new ApiInfoBuilder().title("MarsZhang  接口文档")
                .description("实际样例 REST API")
                .contact(new Contact("Mars Zhang", "blog.csdn.net/makefriend7", "6671605@qq.com"))
                .license("mars 0.1")
                .licenseUrl("blog.csdn.net/makefriend7")
                .version("0.1")
                .build();
    }



    @Bean
    public GsonHttpMessageConverter gsonHttpMessageConverter() {
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
        converter.setGson(new GsonBuilder().registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter()).create());
        return converter;
    }
}
